import Vue from "vue"
import config from "../config.js"

// 20240905迭代版本
export default (url, method, header, token, load, params) => {
	return new Promise((resolve, reject) => {
		const token1 = uni.getStorageSync('token')
		// if (load) {
		// 	uni.showLoading({ title: "加载中" })
		// }
		if (token && !token1) {
			uni.showToast({ title: "请登录", icon: "error" })
			setTimeout(() => {
				uni.navigateTo({ url: '/pages/login/login' })
			}, 1500)
			return
		}
		if (token) {
			var data = { ...params, token: token1 }
		} else {
			var data = { ...params, }
		}
		// console.log(data);
		uni.request({
			url: config.HTTP_QQT + url,
			method: method,
			header: { 'Content-Type': header || 'application/x-www-form-urlencoded' },
			data,
			success: (res) => {
				if (load) {
					uni.hideLoading()
				}
				resolve(res.data);
				// console.log(res)
			},
			fail: (err) => {
				reject(err)
			},
			complete: (res) => {
				if (res.statusCode == 404) {
					console.log(Vue)
					uni.showToast({ title: "请求资源不存在", icon: "error" })
				}
				if (res.statusCode == 500) {
					console.log(Vue)
					uni.showToast({ title: "服务器错误", icon: "error" })
				}
				if (res.statusCode == 401) {
					uni.showToast({ title: "请登录", icon: "error" })
					setTimeout(() => {
						uni.navigateTo({ url: '/pages/login/login' })
					}, 1500)
				}
			}
		})
	})
}